/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * lieDetectionGUI.java
 *
 * Created on Aug 6, 2012, 2:21:19 PM
 */
package com.elicitor.lieDetection.fromDeviation;

import com.elicitor.core.Graphihcs;
import com.elicitor.core.PraatCall;
import com.elicitor.core.SimpleBarChart;
import com.elicitor.listner.MicRecorder;
import com.elicitor.listner.WaveData;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.swing.JFrame;

/**
 *
 * @author dileepa
 */



public class lieDetectionGUI extends javax.swing.JFrame {

    /** Creates new form lieDetectionGUI */
    public lieDetectionGUI() {
        initComponents();
        trueFalseMap= new HashMap<String, List<Double>>();
        deviationResultMap = new HashMap<String, Double>();
        
        startRecording.setVisible(true);
        endRecording.setVisible(false);
        saveData.setVisible(false);
        graphic.setVisible(false);
        
    }
    MicRecorder mr;
    String fileLocation;
    HashMap <String, List<Double>> trueFalseMap; 
    LieDetectionDeviationAlgo algo;
    HashMap <String, Double> deviationResultMap ;
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        startRecording = new javax.swing.JButton();
        endRecording = new javax.swing.JButton();
        saveData = new javax.swing.JButton();
        graphic = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        startRecording.setText("Start Recording");
        startRecording.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startRecordingActionPerformed(evt);
            }
        });

        endRecording.setText("End Recording");
        endRecording.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                endRecordingActionPerformed(evt);
            }
        });

        saveData.setText("Save Data");
        saveData.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveDataActionPerformed(evt);
            }
        });

        graphic.setText("Graphic");
        graphic.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                graphicActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(138, 138, 138)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(graphic, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(saveData, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(endRecording, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(startRecording, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(143, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(93, 93, 93)
                .addComponent(startRecording)
                .addGap(18, 18, 18)
                .addComponent(endRecording)
                .addGap(18, 18, 18)
                .addComponent(saveData)
                .addGap(18, 18, 18)
                .addComponent(graphic)
                .addContainerGap(33, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void startRecordingActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startRecordingActionPerformed

    mr = new MicRecorder(new AudioFormat(22000, 16, 2, true, true));
    mr.start();
    startRecording.setVisible(false);
    endRecording.setVisible(true);
    saveData.setVisible(false);
    graphic.setVisible(false);
    
   
}//GEN-LAST:event_startRecordingActionPerformed

private void endRecordingActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_endRecordingActionPerformed

    
    startRecording.setVisible(false);
    endRecording.setVisible(false);
    saveData.setVisible(true);
    graphic.setVisible(false);
    mr.stop();
    WaveData wd = new WaveData();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException ex) {
            Logger.getLogger(lieDetectionGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    fileLocation=wd.saveToFile("clip0", AudioFileFormat.Type.WAVE, mr.getAudioInputStream()); 
    
        PraatCall prattcall = new PraatCall(fileLocation);
        prattcall.createNewVoiceFile();
        prattcall.sendPraat();  
        
        
}//GEN-LAST:event_endRecordingActionPerformed

private void saveDataActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveDataActionPerformed

    algo = new LieDetectionDeviationAlgo("info.txt");
    startRecording.setVisible(true);
    endRecording.setVisible(false);
    saveData.setVisible(false);
    
    algo.exatractDataToMap("clip"+trueFalseMap.size(),trueFalseMap);
    
    if (trueFalseMap.size()>1){
    
    deviationResultMap =algo.calculateDeviationofVoice(trueFalseMap);
    System.out.println("Deviation............... "+deviationResultMap );
    graphic.setVisible(true);
    }
    
    
}//GEN-LAST:event_saveDataActionPerformed

private void graphicActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_graphicActionPerformed
// TODO add your handling code here:
//    algo.calculateDeviationofVoice(trueFalseMap);
  JFrame frame = new JFrame();
  frame.setSize(350, 300);
  Double deviationValues [] = new Double[deviationResultMap.size()];
  String clipName [] = new String[deviationResultMap.size()];
  
  int i=0;
  for (String key:deviationResultMap.keySet()){
      deviationValues[i] = deviationResultMap.get(key);
      clipName[i] = key;
      i++;
  }
  
  frame.getContentPane().add(new SimpleBarChart(deviationValues, clipName,
    "Voice Deviation Results........"));
 
  frame.setVisible(true);
    
}//GEN-LAST:event_graphicActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(lieDetectionGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(lieDetectionGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(lieDetectionGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(lieDetectionGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new lieDetectionGUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton endRecording;
    private javax.swing.JButton graphic;
    private javax.swing.JButton saveData;
    private javax.swing.JButton startRecording;
    // End of variables declaration//GEN-END:variables
}
